package com.xrui.kafka.mgrutil

import scala.collection.Map

object CommandLine {
    def parseArgs(args: Array[String]): Map[String, String] = {
        args match {
            case Array() => Map.empty
            case Array(v) => Map(v -> "true")
            case _ =>
                val pairs = args.sliding(2)
                pairs.map {
                    case Array(k, v) if k.startsWith("--") && v.startsWith("--") => (k.substring(2), "true")
                    case Array(k, v) if k.startsWith("--") =>
                        (k.substring(2), v.stripPrefix("\"").stripSuffix("\""))
                    case Array(_, v) if v.startsWith("--") => (v.substring(2), "true")
                    case _ => null
                }.filter(_ != null).toMap
        }
    }
}
